package com.edge.pbx.view.summing;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.edge.pbxdsp.R;
import com.qztech.utils.YzDebug;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


/**
 * Copyright (C), 深圳市东乘电子科技有限公司
 * Carate By YangZhong
 */
public class PbxOutputSelectChannelView extends View implements View.OnClickListener {
    private static final float mEdgeScale = 63f / 202f;
    private static final float mCenterScale = 76f / 202f;
//    private Drawable mDrawableBGN;
    private List<Drawable> mDrawableNums;
    private Drawable mDrawableName;
    private int mChannelIndex;
    private int mNumWidth, mNumHeight;
    private Set<Integer> mChsets = new HashSet<>();
    private String mName ;
    private Paint mTextPaint;
    private int mCenterX = 0;
    private int mTextNameTop ;
    public PbxOutputSelectChannelView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setClickable(true);
        mDrawableNums = new ArrayList<>();

        final Resources res = getResources();
//        mDrawableBGN = res.getDrawable(R.drawable.pbx_summing_output_channel_bg_n);
//        mDrawableBGN = res.getDrawable(R.drawable.pbx_summing_output_channel_bg);
//        sColorSelected = res.getColor(R.color.btn_pressed_color);
        mDrawableNums.add(res.getDrawable(R.drawable.pbx_summing_output_num1));
        mDrawableNums.add(res.getDrawable(R.drawable.pbx_summing_output_num2));
        mDrawableNums.add(res.getDrawable(R.drawable.pbx_summing_output_num3));
        mDrawableNums.add(res.getDrawable(R.drawable.pbx_summing_output_num4));
        mDrawableNums.add(res.getDrawable(R.drawable.pbx_summing_output_num5));
        mDrawableNums.add(res.getDrawable(R.drawable.pbx_summing_output_num6));
        mDrawableNums.add(res.getDrawable(R.drawable.pbx_summing_output_num7));
        mDrawableNums.add(res.getDrawable(R.drawable.pbx_summing_output_num8));
        setOnClickListener(this);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.PbxOutputSelectChannelView);
        mDrawableName = array.getDrawable(R.styleable.PbxOutputSelectChannelView_pbx_poscv_name_image);
        mName = array.getString(R.styleable.PbxOutputSelectChannelView_pbx_poscv_name_text);
        mChannelIndex = array.getInteger(R.styleable.PbxOutputSelectChannelView_pbx_poscv_channel_index, 0);
        array.recycle();

        setBackgroundResource(R.drawable.pbx_summing_output_channel_bg);
        mTextPaint = new Paint();
        mTextPaint.setStyle(Paint.Style.FILL_AND_STROKE);

        mTextPaint.setColor(Color.WHITE);
        mTextPaint.setFakeBoldText(true);
            mTextPaint.setTextScaleX(1.2f);

    }

    private final int BASE_W = 302;
    private final int BASE_H = 202;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        float wScale = (float) width / BASE_W;
        float hScale = (float) height / BASE_H;
        float scale = Math.min(wScale, hScale);
//        YzDebug.d("Yang"," w:"+width+" h:"+ height +"  ow:"+(BASE_W*scale) +" oh:"+(BASE_H*scale) );
        setMeasuredDimension((int) (BASE_W * scale), (int) (BASE_H * scale));
    }
    private int mWidth, mHeight;
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mWidth = right - left;
        mHeight = bottom - top;
        if (changed) {
            mNumWidth = (int) (mCenterScale * mHeight);
            mNumHeight = (int) (mEdgeScale * mHeight);
            float textSize = mNumWidth* 0.7f;
            mTextPaint.setTextSize(textSize);
            mTextPaint.setStrokeWidth(textSize*0.07f);
            mTextPaint.setTextAlign(Paint.Align.CENTER);
//            mDrawableBGN.setBounds(0, 0, mWidth, mHeight);
            if (mDrawableName != null)
                mDrawableName.setBounds(0, mNumHeight, mWidth, mHeight - mNumHeight);
            Paint.FontMetrics metrics = mTextPaint.getFontMetrics();

            int height1 = (int) (mNumWidth/2 - metrics.descent +(metrics.bottom-metrics.top)/2);
//            YzDebug.d("Yang",  " height1:"+height1+" " + mTextPaint.getTextSize());
            mTextNameTop = (int) (mNumHeight + height1*0.98f);
            mCenterX = mWidth/2;

        }
    }


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
//        mDrawableBGN.setState(isSelected() ? SELECTED_STATE_SET : EMPTY_STATE_SET);
        postInvalidate();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int i = 0;
        Drawable drawable;
        int x = 0, y = 0;
        for (Integer ch : mChsets) {
            if (ch >= 0 && ch < mDrawableNums.size()){
                drawable = mDrawableNums.get(ch);
//                x = (i % 4) * mNumWidth;
//                y = (i / 4) * (mNumHeight+mNumWidth);
                 x = (ch % 4) * mNumWidth;
                y = (ch / 4) * (mNumHeight+mNumWidth);
                drawable.setBounds(x, y, x+mNumWidth, y + mNumHeight);
                drawable.draw(canvas);
                i++;
            }
        }


        if (mName != null){
            canvas.save();


            canvas.drawText(mName, mCenterX,mTextNameTop,mTextPaint);
            canvas.restore();
        }else{
            if (mDrawableName != null)
                mDrawableName.draw(canvas);
        }
    }

    public void addSum(int num){
        if (mChsets.add(num)){
            postInvalidate();
        }
    }
    public void removeSum(int num){
        if (mChsets.remove(num)){
            postInvalidate();
        }
    }
    public void initSums(int[] nums){
        if (nums == null) return;
        mChsets.clear();

        int i = 0;
        for (int num : nums) {
            if (num == 1)
                mChsets.add(i);
            i++;
        }
        postInvalidate();
    }
    public void clearSum(){
        mChsets.clear();
        postInvalidate();
    }
    public int getChannelIndex() {
        return mChannelIndex;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onClick(View v) {
        if (!isSelected()) {
            setSelected(true);
            if (mOnCheckChangeListener != null) {
                mOnCheckChangeListener.onCheckChange(this, true);
            }
        }
//        setSelected(!isSelected());
//        if (mOnCheckChangeListener != null){
//            mOnCheckChangeListener.onCheckChange(this, isSelected());
//        }
    }

    private OnCheckChangeListener mOnCheckChangeListener;

    public void setOnCheckChangeListener(OnCheckChangeListener onCheckChangeListener) {
        mOnCheckChangeListener = onCheckChangeListener;
    }

    public interface OnCheckChangeListener {
        void onCheckChange(PbxOutputSelectChannelView view, boolean isChecked);
    }
    public void setName(String name){
        this.mName = name;
        postInvalidate();
    }
}
